//
//  prototipo1MoviesViewController.m
//  prototipo1Movies
//
//  Created by mayraDonaji on 9/25/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "prototipo1MoviesViewController.h"
#import "recomenda.h"
#import "noticias.h"
#import "prox.h"
#import "busqueda.h"
#import "Movie.h"
#import "help.h"

@implementation prototipo1MoviesViewController

@synthesize recomenda, busqueda, prox, noticias, help, mDB, scrollView, pageControl, pageControlIsChangingPage;


/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	
	//animacion
	UIImage * img1 = [UIImage imageNamed:@"palomas0001.jpg"];
	UIImage * img2 = [UIImage imageNamed:@"palomas0002.jpg"];
	UIImage * img3 = [UIImage imageNamed:@"palomas0003.jpg"];
	UIImage * img4 = [UIImage imageNamed:@"palomas0004.jpg"];
	UIImage * img5 = [UIImage imageNamed:@"palomas0005.jpg"];
	UIImage * img6 = [UIImage imageNamed:@"palomas0006.jpg"];
	UIImage * img7 = [UIImage imageNamed:@"palomas0007.jpg"];
	UIImage * img8 = [UIImage imageNamed:@"palomas0008.jpg"];
	UIImage * img9 = [UIImage imageNamed:@"palomas0009.jpg"];
	UIImage * img10 = [UIImage imageNamed:@"palomas0010.jpg"];
	UIImage * img11 = [UIImage imageNamed:@"palomas0011.jpg"];
	UIImage * img12 = [UIImage imageNamed:@"palomas0012.jpg"];
	UIImage * img13 = [UIImage imageNamed:@"palomas0013.jpg"];
	UIImage * img14 = [UIImage imageNamed:@"palomas0014.jpg"];
	UIImage * img15 = [UIImage imageNamed:@"palomas0015.jpg"];
	UIImage * img16 = [UIImage imageNamed:@"palomas0016.jpg"];
	UIImage * img17 = [UIImage imageNamed:@"palomas0017.jpg"];
	UIImage * img18 = [UIImage imageNamed:@"palomas0018.jpg"];
	UIImage * img19 = [UIImage imageNamed:@"palomas0019.jpg"];
	UIImage * img20 = [UIImage imageNamed:@"palomas0020.jpg"];
	UIImage * img21 = [UIImage imageNamed:@"palomas0021.jpg"];
	UIImage * img22 = [UIImage imageNamed:@"palomas0022.jpg"];
	UIImage * img23 = [UIImage imageNamed:@"palomas0023.jpg"];
	UIImage * img24 = [UIImage imageNamed:@"palomas0024.jpg"];
	UIImage * img25 = [UIImage imageNamed:@"palomas0025.jpg"];
	UIImage * img26 = [UIImage imageNamed:@"palomas0026.jpg"];
	UIImage * img27 = [UIImage imageNamed:@"palomas0027.jpg"];
	UIImage * img28 = [UIImage imageNamed:@"palomas0028.jpg"];
	UIImage * img29 = [UIImage imageNamed:@"palomas0029.jpg"];
	UIImage * img30 = [UIImage imageNamed:@"palomas0030.jpg"];
	UIImage * img31 = [UIImage imageNamed:@"palomas0030.jpg"];
	UIImage * img32 = [UIImage imageNamed:@"palomas0030.jpg"];
	UIImage * img33 = [UIImage imageNamed:@"palomas0030.jpg"];
	
	UIImageView * myImageView = [[UIImageView alloc] initWithFrame: [[UIScreen	mainScreen] bounds]];
	myImageView.animationImages = [NSArray arrayWithObjects:img1,img2,img3,img4,img5,img6,img7,img8,img9,
								   img10,img11,img12,img13,img14,img15,img16,img17,img18,img19,img20,img21,
								   img22,img23,img24,img25,img26,img27,img28,img29,img30,img31, img32, img33, nil];
	
	myImageView.animationDuration = 5;
	myImageView.animationRepeatCount = 1;
	[myImageView startAnimating];

	
	
	mDB = [[DBDelegate alloc] init];
	//[mDB readMoviesFromDatabase];
	//[mDB readEstrenosFromDatabase];
	//[mDB readTopFromDatabase];
	
	[self.view addSubview:myImageView];
	[myImageView release];
	[self setupPage];
    [super viewDidLoad];
	
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

-(IBAction) recomenda :(id)sender{
	recomenda * recomiendas = [[recomenda alloc]initWithNibName: nil bundle:nil];
	recomiendas.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
	recomiendas.input = [self mDB];
	[mDB autorelease];
	[self presentModalViewController:recomiendas animated:YES];	
	
}
-(IBAction) busqueda :(id)sender{
	busqueda * busquedas = [[busqueda alloc]initWithNibName: nil bundle:nil];
	busquedas.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
	busquedas.input = [self mDB];
	[mDB autorelease];
	[self presentModalViewController:busquedas animated:YES];	

}
-(IBAction) prox :(id)sender{
	prox * proxi = [[prox alloc]initWithNibName: nil bundle:nil];
	proxi.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;	
	proxi.input = [self mDB];
	[mDB autorelease];
	[self presentModalViewController:proxi animated:YES];	

}
-(IBAction) noticias :(id)sender{
	noticias * noticia = [[noticias alloc]initWithNibName: nil bundle:nil];
	noticia.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
	noticia.input = [self mDB];
	[mDB autorelease];
	[self presentModalViewController:noticia animated:YES];	

}

-(IBAction) help :(id)sender{
	help * helps = [[help alloc]initWithNibName: nil bundle:nil];
	helps.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
	[self presentModalViewController:helps animated:YES];
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[mDB dealloc];
    [super dealloc];
}

- (void)setupPage
{
	
	scrollView.delegate = self;
	
	//[self.scrollView setBackgroundColor:[UIColor blackColor]];
	[scrollView setCanCancelContentTouches:NO];
	
	scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
	scrollView.clipsToBounds = YES;
	scrollView.scrollEnabled = YES;
	scrollView.pagingEnabled = YES;
	
	NSMutableArray *topMovies = mDB.top;
	NSInteger size = [topMovies count];
	NSLog(@"%i", size);
	CGFloat cx = 0;
	for (NSUInteger i = 0, max = [topMovies count]; i < max; i++) {
		Movie *thisMovie = [topMovies objectAtIndex:i];

		// Getting the URL of the image
		NSString *urlString = thisMovie.image;
		
		// Creating the rectangle for placing the image
		CGRect rect = CGRectMake(10+cx, 0, scrollView.frame.size.width * 0.43, scrollView.frame.size.height*0.6);
		
		
		// - Showing Images
		NSString *embedHTML = @"\
		<html><head>\
		<style type=\"text/css\">\
		body {\
		background-color: transparent;\
		color: white;\
		}\
		</style>\
		</head><body style=\"margin:0\">\
		<img src=\"%@\" width=\"%0.0f\" height=\"%0.0f\"/>\
		</body></html>";
		
		NSString *html = [NSString stringWithFormat:embedHTML, urlString, rect.size.width, rect.size.height];
		// - Creating the view of the image
		UIWebView *videoView = [[UIWebView alloc] initWithFrame:rect];
		[videoView loadHTMLString:html baseURL:nil];
		[scrollView addSubview:videoView];
		[videoView release];
		
		// - Creating the title label
		CGRect titleRect = CGRectMake(scrollView.frame.size.width * 0.43+30+cx, 0, scrollView.frame.size.width * 0.45, scrollView.frame.size.height*0.6); 
		UILabel * titleLabel = [[UILabel alloc] initWithFrame:titleRect];
		titleLabel.text = thisMovie.title;
		titleLabel.textColor = [UIColor blackColor];
		titleLabel.backgroundColor = [UIColor clearColor];
		titleLabel.lineBreakMode = UILineBreakModeWordWrap;
		titleLabel.numberOfLines = 0;
		[scrollView addSubview:titleLabel];
		[titleLabel release];
		
		// - Creating synopsis label
		CGRect synopsisRect = CGRectMake(cx + scrollView.frame.size.width * 0.05, scrollView.frame.size.height*0.63, scrollView.frame.size.width * 0.9, scrollView.frame.size.height*0.3); 
		UILabel * synopsisLabel = [[UILabel alloc] initWithFrame:synopsisRect];
		synopsisLabel.text = thisMovie.synopsis;
		synopsisLabel.textColor = [UIColor blackColor];
		synopsisLabel.font = [UIFont fontWithName:@"Arial" size:(13.0)];
		synopsisLabel.backgroundColor = [UIColor clearColor];
		synopsisLabel.lineBreakMode = UILineBreakModeWordWrap;
		synopsisLabel.numberOfLines = 0;
		[scrollView addSubview:synopsisLabel];
		[synopsisLabel release];
		
		
		cx += scrollView.frame.size.width;
	}

	/*
	 // -- Original implementation --
	NSUInteger nimages = 0;
	CGFloat cx = 0;
	for (; ; nimages++) {
		
		// - Images in Scroll View -
		NSString *imageName = [NSString stringWithFormat:@"image%d.jpg", (nimages + 1)];
		UIImage *image = [UIImage imageNamed:imageName];
		if (image == nil) {
			break;
		}
		UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
		
		CGRect rect = imageView.frame;
		rect.size.height = image.size.height;
		rect.size.width = image.size.width;
		rect.origin.x = ((scrollView.frame.size.width - image.size.width) / 2) + cx;
		rect.origin.y = ((scrollView.frame.size.height - image.size.height) / 2);
		
		imageView.frame = rect;
		 
		
		//[scrollView addSubview:imageView];
		//[self embedYouTube:@"http://www.youtube.com/watch?v=WL2l_Q1AR_Q" frame:CGRectMake(20, 20, 100, 100)];
		
		//NSString *urlString = @"http://www.youtube.com/v/WL2l_Q1AR_Q";
		NSString *urlString = @"http://codeit.com.mx/images/integrantes/david.jpg";
		
		
		// -For showing YouTube Videos
		NSString *embedHTML = @"\
		<html><head>\
		<style type=\"text/css\">\
		body {\
		background-color: transparent;\
		color: white;\
		}\
		</style>\
		</head><body style=\"margin:0\">\
		<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
		width=\"%0.0f\" height=\"%0.0f\"></embed>\
		</body></html>";
		
		
		// - Showing Images
		NSString *embedHTML = @"\
		<html><head>\
		<style type=\"text/css\">\
		body {\
		background-color: transparent;\
		color: white;\
		}\
		</style>\
		</head><body style=\"margin:0\">\
		<img src=\"%@\" width=\"%0.0f\" height=\"%0.0f\"/>\
		</body></html>";
		
		NSString *html = [NSString stringWithFormat:embedHTML, urlString, rect.size.width, rect.size.height];
		UIWebView *videoView = [[UIWebView alloc] initWithFrame:rect];
		[videoView loadHTMLString:html baseURL:nil];
		[scrollView addSubview:videoView];
		[videoView release];
		
		[imageView release];
		
		cx += scrollView.frame.size.width;
	}
	*/
	
	self.pageControl.numberOfPages = [topMovies count];
	[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
}

- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
    if (pageControlIsChangingPage) {
        return;
    }
	
	/*
	 *	We switch page at 50% across
	 */
    CGFloat pageWidth = _scrollView.frame.size.width;
    int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControl.currentPage = page;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView 
{
    pageControlIsChangingPage = NO;
}

#pragma mark -
#pragma mark PageControl stuff
- (IBAction)changePage:(id)sender 
{
	/*
	 *	Change the scroll view
	 */
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * pageControl.currentPage;
    frame.origin.y = 0;
	
    [scrollView scrollRectToVisible:frame animated:YES];
	
	/*
	 *	When the animated scrolling finishings, scrollViewDidEndDecelerating will turn this off
	 */
    pageControlIsChangingPage = YES;
}

- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame {
	NSString *embedHTML = @"\
    <html><head>\
	<style type=\"text/css\">\
	body {\
	background-color: transparent;\
	color: white;\
	}\
	</style>\
	</head><body style=\"margin:0\">\
    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
	width=\"%0.0f\" height=\"%0.0f\"></embed>\
    </body></html>";
	NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height];
	UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
	[videoView loadHTMLString:html baseURL:nil];
	[scrollView addSubview:videoView];
	[videoView release];
}


@end
